package util;




import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author luiz
 */
public class Captcha {

    private String frase;
    private BufferedImage buffer;

    public static final int altura = 50;
    public static final int largura = 150;
    public static final Font fonte = new Font("Tahoma", Font.BOLD, 30);
    private static final Color[] COLORS_DEFAULT = {Color.BLUE, Color.MAGENTA,
        Color.GRAY, Color.GREEN, Color.RED, Color.BLACK};

    public Captcha() {
        frase = gerarLetras();
        buffer = new BufferedImage(largura, altura, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = buffer.getGraphics();
        graphics.setFont(fonte);

        limparImagem(graphics);

        for (int i = 0; i < frase.length(); i++) {

            graphics.setColor(gerarCor());
            graphics.drawString("" + frase.charAt(i), 10 + (i * 25 + (int) (11 * Math.random())),  25 + (int) (28 * Math.random()));

        }

        for (int i = 0; i < 50; i++) {
            graphics.setColor(gerarCor());
            linhasAleatorias(graphics, largura+100, altura+50);
        }
        buffer.flush();
    }

    private void limparImagem(Graphics graphics) {
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, largura, altura);
    }

    private Color gerarCor() {
        return COLORS_DEFAULT[(int) (Math.random() * 5)];
    }

    private String gerarLetras() {
        String frase = "";
        for (int i = 0; i < 5; i++) {
            char letra = (char) (((int) 'A') + Math.random() * 25 /* 58 */);
            frase += letra;
        }
        return frase;
    }

    private void linhasAleatorias(Graphics graphics, int largura, int altura) {
        graphics.drawLine((int) (Math.random() * largura),
                (int) (Math.random() * largura),
                (int) (Math.random() * altura), (int) (Math.random() * altura));
    }

    public void salvar(File file) throws IOException {
        FileOutputStream output = new FileOutputStream(file);
        String extension = file.getName();
        extension = extension.substring(extension.indexOf('.') + 1);
        ImageIO.write(buffer, extension, output);
        output.close();
    }

    public byte[] getBytes() throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        ImageIO.write(buffer, "png", output);
        byte[] data = output.toByteArray();
        output.close();
        return data;
    }

    public String getFrase() {
        return frase;
    }
}
